<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

// 浏览历史
class history extends API_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('history_model','mhistory');
	}
	/**
	 * 添加搜索记录
	 */
	public function hcreate() {
		$this->_auto();
		$create_data = array(
			'uid' => $this->userinfo['id'],
			'title' => $this->data['kw'],
			'timeline' => time()
		);
		$count = $this->mhistory->get_count_all(array('uid'=>$this->userinfo['id']));
		if($count >= 20) {
			$logs = $this->mhistory->get_list(19,0,array('id'=>'desc'),array('uid'=>$this->userinfo['id']),'id');
			$ids = array();
			foreach($logs as $log) {
				$ids[] = $log['id'];
			}
			$this->mhistory->dels($ids);
		}
		$data = $this->mhistory->create($create_data);
		if ($data) {
			// 返回成功
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '添加成功';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = $data;
		} else {
			// 返回失败
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '操作失败';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = '';
		}

		$this->_send_json($this->vdata);
	}
	/**
	 * 删除
	 */
	public function hdel() {
		if(isset($this->data['id']) && $this->data['id']) {
			$del = $this->mhistory->dels($this->data['id']);
		} else {
			$del = $this->mhistory->dels('',array('uid'=>$this->userinfo['id']));
		}
		if ($del) {
			// 返回成功
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '删除成功';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = $del;
		} else {
			// 返回失败
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '操作失败';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = '';
		}

		$this->_send_json($this->vdata);
	}
	/**
	 * 列表
	 */
	public function hlist() {
		$list = $this->mhistory->get_list(10,0,array('id'=>'desc'),array('uid'=>$this->userinfo['id']),'id,title');
		if ($list) {
			// 返回成功
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '删除成功';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = $list;
		} else {
			// 返回失败
			$this->vdata['returnCode'] = '200';
			$this->vdata['returnInfo'] = '操作失败';
			$this->vdata['secure'] = JSON_SECURE;
			$this->vdata['content'] = '';
		}

		$this->_send_json($this->vdata);
	}

}
